/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Edge;
import evaluation.Graph;
import evaluation.Vertex;
import java.util.LinkedHashSet;
import logic.Type;

/**
 *
 * @author phong
 */
public abstract class DRSUnaryComplexCondition extends DRSComplexCondition {
    
    DrtExpression term;
    
    public DRSUnaryComplexCondition(DrtExpression term) {
        this.term = term;
        
        if (!term.getType().matches(Type.TRUTH_TYPE))
            throw new Error("incorrect type");
    }
    
    public DrtExpression getTerm() {
        return this.term;
    }
    
    @Override
    public void getRefs(LinkedHashSet<DRSReferent> refs) {
        this.term.getRefs(refs);
    }
    
    @Override
    public void getAllVariables(LinkedHashSet<DrtVariable> vars) {
        this.term.getAllVariables(vars);
    }    
    
    @Override
    public Graph toGraph() {
        
        Graph gt = this.term.toGraph();
        
        Graph g = new Graph();
        
        LinkedHashSet<DRSReferent> rs = new LinkedHashSet<DRSReferent>();
        this.term.getRefs(rs);

        Vertex p, l; 
        l = new Vertex(Naming.getUniqueName("l0"), Vertex.TYPE_LABEL);
        if (this instanceof DRSInworldCondition)
            p = new Vertex(((DRSInworldCondition)this).getRef().getName(), Vertex.TYPE_REF);
        else
            p = null;
        
        Vertex opt;
        if (this instanceof DRSPossibilityCondition)
            opt = Vertex.createPos();
        else if (this instanceof DRSNecessityCondition)
            opt = Vertex.createNec();
        else if (this instanceof DRSNegatedCondition)
            opt = Vertex.createNeg();
        else 
            opt = Vertex.createIs();
        
        g.addMarks(opt);
        g.merge(gt);
        
        for (Vertex v : gt.getMarks()) {
            g.addEdge(new Edge("", v, l));
        }
        g.addEdge(new Edge("", l, opt));
        
        if (p != null)
            g.addEdge(new Edge("", p, opt));
        
        return g;
    }

    @Override
    public int hashCode() {
        return this.term.hashCode() * 31 + this.getClass().toString().hashCode();
    }
}
